<?php


namespace App\Http\Controllers;


use App\Models\Permission;
use App\Services\RoleService;
use App\Transformer\RoleTransformer;
use Dingo\Api\Http\Request;

class RoleController extends Controller
{

    /**
     * 角色列表
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function search(Request $request)
    {
        return $this->response->paginator(RoleService::search($request), new RoleTransformer());
    }

    /**
     * 角色详情
     * @param $id
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function show($id)
    {
        return $this->response->item(RoleService::info($id), new RoleTransformer());
    }

    /**
     * 创建角色
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function create(Request $request)
    {
        can('role_create');
        return $this->response->item(RoleService::createAndUpdate($request, 0), new RoleTransformer());
    }

    /**
     * 修改用户
     * @param Request $request
     * @param $id
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function update(Request $request, $id)
    {
        can('role_update');
        return $this->response->item(RoleService::createAndUpdate($request, $id), new RoleTransformer());
    }

    /**
     * 角色删除
     * @param $id
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function delete($id)
    {
        can('role_delete');
        RoleService::delete($id);
        return $this->response->noContent();
    }

    /**
     * 获取权限树
     * @author thanatos thanatos915@163.com
     */
    public function permissions()
    {
        $model = new Permission();
        return $this->resArray($model->defaultPermission());
    }
}
